<template>
  <div class="page-content">
    <hy-table
      :listData="datalist"
      :listCount="dataCount"
      v-model:page="pageInfo"
      v-bind="contentTableConfig"
    >
      <!-- headerHandler插槽 -->
      <template #headerHandler>
        <el-button type="primary" size="mini" @click="handleCreateClick"
          >新建</el-button
        >
      </template>
      <!-- 2.列中的插槽 -->
      <template #status="scope">
        <el-button
          plain
          size="mini"
          :type="scope.row.enable ? 'success' : 'danger'"
        >
          {{ scope.row.enable ? "启用" : "禁用" }}
        </el-button>
      </template>
      <template #createAt="scope">
        <el-tag size="mini" type="success">{{
          $filters.formatTime(scope.row.createAt)
        }}</el-tag>
      </template>
      <template #updateAt="scope">
        <el-tag size="mini" type="success">{{
          $filters.formatUtcString(scope.row.updateAt)
        }}</el-tag>
      </template>
      <template #handler="scope">
        <el-button type="text" size="mini" @click="handleEditClick(scope.row)"
          >编辑</el-button
        >
        <el-button type="text" size="mini" @click="handleDeleteClick(scope.row)"
          >删除</el-button
        >
      </template>
      <!-- 3.额外的列插槽 -->
      <template
        v-for="propItem in otherPropSlots"
        :key="propItem.prop"
        #[propItem.slotName]="scope"
      >
        <template v-if="propItem.slotName">
          <slot :name="propItem.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </hy-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from "vue"
import HyTable from "@/base-ui/table"
import { useStore } from "@/store"
export default defineComponent({
  props: {
    contentTableConfig: {
      type: Object,
      default: () => ({})
    },
    pageName: {
      type: String,
      required: true
    }
  },
  components: {
    HyTable
  },
  emits: ["newBtnClick", "editBtnClick"],
  setup(props, { emit }) {
    const store = useStore()
    const pageInfo = ref({
      currentPage: 0,
      pageSize: 10
    })
    // 监测pageInfo数据变化请求数据
    watch(
      () => pageInfo.value,
      () => {
        getPageData()
      }
    )
    const getPageData = (queryInfo?: any) => {
      // 获取用户列表
      store.dispatch("system/getPageListAction", {
        pageName: props.pageName,
        queryInfo: {
          offset: pageInfo.value.currentPage,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()
    // 从vuex中拿数据
    const datalist = computed(() => {
      return store.getters["system/pageListData"](props.pageName)
    })
    const dataCount = computed(() => {
      return store.getters["system/pageListCount"](props.pageName)
    })
    // 获取contentTableConfig.propList中除了status、createAt、updateAt、handler插槽之外的其他插槽
    const otherPropSlots = props.contentTableConfig?.propList.filter(
      (item: any) =>
        item.slotName !== "status" &&
        item.slotName !== "createAt" &&
        item.slotName !== "updateAt" &&
        item.slotName !== "handler"
    )
    // 新建
    const handleCreateClick = () => {
      emit("newBtnClick")
    }
    // 编辑
    const handleEditClick = (row: any) => {
      emit("editBtnClick", row)
    }
    return {
      datalist,
      pageInfo,
      dataCount,
      otherPropSlots,
      getPageData,
      handleCreateClick,
      handleEditClick
    }
  }
})
</script>

<style lang="less"></style>
